<?php 
session_start();
define("CCTRL", "settings");
define("CCTRLNAME", "settings");

class settings extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       private $_configs = null;
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('smarty3');
            $this->load->model('APanel/setting_model');
            $this->setting_model=new setting_model();
            include APPPATH . 'libraries/defu.php';
            $this->smarty=new CI_Smarty3();$this->smarty->error_reporting = E_ALL & ~E_NOTICE;
        }
        function Init(){
            $data=$this->setting_model->gets();
            foreach ($data as $v){
                $_SESSION["ADP"]["SETTINGS"][$v->Key]=$v->Value;
            }
        }
        public function index(){
            if(!checkAuthority("setting")){
                $this->smarty
                        ->assign('_SESSION', $_SESSION)
                        ->display("sys/04_access_denied");
                return;
            }
            $tmp=$this->setting_model->gets();
            if(count($tmp)>0)foreach ($tmp as $s){
                $Data["Settings"][$s->Key]=$s->Value;
            }
            //var_dump($Data["Settings"]);
            $this->smarty
                    ->assign('_SESSION', $_SESSION)
                    ->assign('Data', $Data)
                    ->display("APanel/settings/system");
	}
        function i(){
            //            $aParamsi=array(
//                "Key"=>"check-out-to-email",
//                "Value"=>"khuongxuantruong@gmail.com",
//                "Type"=>"text",
//                "Name"=>"Email nhận thông báo!",
//            );
//            $aParamsu=array(
//                "Value",
//                "Type",
//                "Name"
//            );
//            $this->setting_model->insert_onduplicate_update($aParamsi,$aParamsu);
        }
        function save(){
            if($this->input->post("Key")===false){
                echo json_encode(array("code"=>-1,"msg"=>"Bad request!"));
            }
            if($this->input->post("Value")===false){
                echo json_encode(array("code"=>-1,"msg"=>"Bad request!"));
            }
//            if (!preg_match('/^[a-z0-9._,-]+$/', $this->input->post("Value"))) {
//                $code = -1;
//                $msg = "Chỉ cho phép các ký tự (a – z), số (0 – 9) và dấu chấm,dấu phảy.";
//                echo json_encode(array("code"=>$code,"msg"=>$msg));
//                return;
//            }
            
            $aParams["Key"]=  convertUrl($this->input->post("Key"));
            $aParams["Value"]= strip_tags($this->input->post("Value"));
            if($this->input->post("Name")!==false){
                $aParams["Name"]=$this->input->post("Name");
            }
            $aParamsu=array("Value");
            $rs=$this->setting_model->insert_onduplicate_update($aParams,$aParamsu);
            //$this->setting_model->updateByKey($this->input->post("Key"), $aParams)
            if($rs){
                $code=1;
                $msg="[Success] Thành công.";
            }else{
                $code=1;
                $msg="[Fail] Thất bại vui lòng thử lại.";
            }
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        function Delete(){
            $ID=isset($_POST["ID"])?$_POST["ID"]:"";
            if(!checkAuthority(CCTRL,false)){
                $code=-1;
                $msg="Bạn không có quyền thực hiện thao tác này.<br/> Mọi thay đổi dữ liệu đều không được công nhận.";
            }elseif($this->setting_model->delete($_POST["ID"])){
                $code=1;
                $msg="Success. Item have been deleted.";
            }else{
                $code=-1;
                $msg="Fail. Cant delete this item.";
            }
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        function Edit(){
            
            $ID=isset($_POST["ID"])?$_POST["ID"]:"";
            $row=$this->setting_model->get($ID);
            if(isset($row[0])){
                $row=  objectToArray($row[0]);
            }else{
                $row=null;
            }
            $Data["row"]=$row;
            $this->smarty->assign('_SESSION', $_SESSION);
            $this->smarty->assign('Data', $Data);
            $this->smarty->display("APanel/settings/02_edit");
        }
        
        
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */